// pages/masterStartService/masterStartService.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgPath: app.globalData.baseImgPath,
    backImgPath: app.globalData.URL,
    imgList:[],
    id:0,//订单号
    orderInfo:[],//订单详情
    qrcode:0,//二维码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    that.setData({
      id: options.id //已付款页面取回来的id
    });
    wx.getStorage({
      key: 'sessionId',
      success: function (res) {
        that.setData({
          sessionId: res.data, //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionId);
        wx.request({
          url: app.globalData.urlPath + 'ManagerMent/expressInfo',
          data: {
            sessionid: that.data.sessionId,
            id: that.data.id
          },
          method: "POST",
          success: function (res) {
            console.log(res)
            if(res.data.code==200){
              var data = res.data.data;
              data.createtime = that.totime(data.createtime)
              that.setData({
                orderInfo: data
              })
            }
           else{
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
              if (res.data.code == 5000) {
                wx, wx.redirectTo({
                  url: '/pages/login/login',
                })
              }
           }
          }
        })
      },
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //上传图片
  chooseImg: function () {
    let self = this;
    if (this.data.imgList.length < 9) {
      wx.chooseImage({
        count: 1,
        sourceType: ['album'],
        success: function (res) {
          const tempFilePaths = res.tempFilePaths;
          wx.uploadFile({
            url: app.globalData.urlPath + 'ManagerMent/uploadpic',
            filePath: tempFilePaths[0],
            header: { "Content-Type": "multipart/form-data" },
            name: 'file',
            formData: {
              sessionid: self.data.sessionId
            },
            success: function (res) {
              console.log(res.data)
              let imgdata = JSON.parse(res.data)
              if (imgdata.code == 200) {
                let imgPath = imgdata.data;
                self.setData({ imgList: self.data.imgList.concat(imgPath) })
              } else {
                wx.showToast({
                  title: '上传失败，请稍后重新',
                  icon: 'none'
                })
              }

            },
            fail: function () {
              wx.showToast({
                title: '上传失败，请稍后重新',
                icon: 'none'
              })
            }
          })
          // wx.getFileSystemManager().readFile({
          //   filePath: tempFilePaths[0], //选择图片返回的相对路径
          //   encoding: 'base64', //编码格式
          //   success: res => { //成功的回调
          //     //console.log('data:image/png;base64,' + res.data)
          //     let file = res.data;
          //     self.setData({
          //       // url: 'data:image/jpg;base64,' + base64
          //       url: file
          //     })
          //     wx.showToast({
          //       title: '正在上传...',
          //       icon: 'loading',
          //       mask: true,
          //       //  duration: 5000,
          //       success: function () {
          //         //请求接口
          //         wx.request({
          //           method: "POST",
          //           url: app.globalData.urlPath + 'ManagerMent/base64',
          //           data: {
          //             base64: self.data.url,
          //             sessionid: self.data.sessionId
          //           },
          //           success(res) {
          //             console.log(res)
          //             if(res.data.code==200){
          //               var imgUrl = res.data.data;
          //               self.setData({
          //                 imgList: self.data.imgList.concat(imgUrl)
          //               })
          //              }
          //             else{
          //               wx.showToast({
          //                 title: res.data.error,
          //                 icon:"none",
          //                 duration:2000
          //               })
          //               setTimeout(function(){
          //                 wx.redirectTo({
          //                   url: '/pages/login/login',
          //                 },2000)
          //               })
          //             }           
          //           }
          //         })
          //       }
          //     })
          //   }
          // })
        },
      })
    }
  },
  //删除上传的图片
  deletePic: function (e) {
    var self = this;
    wx.showToast({
      title: '删除成功！',
    })
    var imgs = self.data.imgList;
    console.log(imgs)
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    self.setData({
      imgList: imgs
    });
  },
  //底部导航切换
  handleChangeTab({ detail }) {
    var that=this;
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.redirectTo({
        url: '/pages/masterOrderList/masterOrderList',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          //加载二维码返回数据
          wx.request({
            url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
            data: {
              sessionid: app.globalData.sessionid,
              qrcode: that.data.qrcode
            },
            method: "POST",
            success: function (res) {
              console.log(res)
              if (res.data.code == 200) {
                that.setData({
                  orderInfo: res.data.data,
                  fbrand: res.data.data.fbrand,
                  fmaterial: res.data.data.fmaterial,
                  fsize: res.data.data.fsize,
                })
                wx.redirectTo({
                  url: '../masterSubOrder/masterSubOrder?qrcode=' + that.data.qrcode,
                })
              }
              else {
                wx.showToast({
                  title: res.data.error,
                  icon: 'none',
                  duration: 2000
                })
                if (res.data.code == 5000) {
                  wx, wx.redirectTo({
                    url: '/pages/login/login',
                  })
                }
              }
            }
          })

        }
      })
    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/masterMy/masterMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  //提交订单
  formSubmit: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/finish',
      method: 'POST',
      data: {
        sessionid: that.data.sessionId, //sessionId
        id: that.data.id, //订单id
        ffixedpic: that.data.imgList, //图片列表
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000
          });
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/masterOrderList/masterOrderList?current=4',
            })
          }, 2000)
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  totime:function(t){
    let T = new Date(t * 1000)
    let text = T.getFullYear() + "/" + (T.getMonth() + 1) + "/" + T.getDate() + "  " + T.getHours() + ":" + T.getMinutes();
    return text;
  }
})